/*******************************************************************************
 * Copyright (c) 2014 Francesco Gabbrielli.
 * 
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v2.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * 
 * Contributors:
 *     Francesco Gabbrielli - initial API and implementation
 ******************************************************************************/
package it.francescogabbrielli.android.solidalapp;

import it.francescogabbrielli.android.solidalapp.SolidalApp.DataKey;
import it.francescogabbrielli.android.solidalapp.deviceendpoint.model.Device;
import it.francescogabbrielli.android.solidalapp.file.RequestFileActivity;
import it.francescogabbrielli.android.solidalapp.file.ResponseFileActivity;
import it.francescogabbrielli.android.solidalapp.profileendpoint.model.Profile;
import it.francescogabbrielli.android.solidalapp.requestendpoint.model.Request;
import it.francescogabbrielli.android.solidalapp.responseendpoint.model.Response;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

/**
 * Receiver for {@link EndpointsService}
 * 
 * XXX: update ALWAYS manifest for intent filter...
 */
public class EndpointsReceiver extends BroadcastReceiver {
	
	public EndpointsReceiver() {
	}

	@Override
	public void onReceive(Context context, Intent intent) {
		
		SolidalApp app = (SolidalApp) context.getApplicationContext();
		BaseActivity activity = app.getCurrentActivity();
		
		Log.i(SolidalApp.TAG, "Endpoints receiving: "+intent);
		Log.v(SolidalApp.TAG, "Extras: "+EndpointsUtils.toString(intent.getExtras()));

		// default error management
		if(intent.getStringExtra("error")!=null) {
			switch (IntentAction.valueOf(intent)) {
			case ENDPOINTS_SEND_REQUEST:
			case ENDPOINTS_SEND_RESPONSE:
			case GCM_SEND_REQUEST_CHAT:
			case GCM_SEND_RESPONSE_CHAT:
				//pass
				break;
			default:
				app.alert(intent.getStringExtra("error"));
				return;
			}
		}
		
		Bundle extras = intent.getExtras();
		
		if(activity!=null)
			
			switch(IntentAction.valueOf(intent)) {
			
			case ENDPOINTS_GET_PROFILE: {
				Profile profile = EndpointsUtils.fromBundle(extras, Profile.class);
				if(profile!=null) {
					app.setData(DataKey.PROFILE, profile);
					app.register();
				} else {
					IntentAction.ENDPOINTS_INSERT_PROFILE.start(context);
				}
				break;}
			
			case ENDPOINTS_INSERT_PROFILE:
			case ENDPOINTS_UPDATE_PROFILE: {
				Profile profile = EndpointsUtils.fromBundle(extras, Profile.class);
				if(profile!=null) {
					app.setData(DataKey.PROFILE, profile);
					app.register();
				} else if(app.getData(DataKey.PROFILE)==null) {
					app.alert(R.string.error_server, true);
					Log.e(SolidalApp.TAG, "Unexpected null profile");
				}
				break;}
				
			case ENDPOINTS_CHECK_DEVICE:
				app.setData(DataKey.DEVICE, EndpointsUtils.fromBundle(extras, Device.class));
				break;
				
			case ENDPOINTS_SEND_REQUEST:
				if(intent.hasExtra("error")) {
					app.setData(DataKey.REQUEST, null);
					app.alert(context.getString(R.string.request_sent_error)+"\n"+intent.getStringExtra("error"));
				} else {
					Request r = EndpointsUtils.fromBundle(extras, Request.class);
					Log.i(SolidalApp.TAG, "Request sent: "+r);
					app.setData(DataKey.REQUEST, r);
					app.alert(R.string.request_sent_message);
				}
				break;
				
			case ENDPOINTS_SEND_RESPONSE:
				if(intent.hasExtra("error")) {
					app.setData(DataKey.RESPONSE, null);
					app.alert(app.getString(R.string.response_sent_error)+"\n"+intent.getStringExtra("error"));
				} else {
					Response r = EndpointsUtils.fromBundle(extras, Response.class);
					Log.i(SolidalApp.TAG, "Response sent: "+r);
					app.setData(DataKey.RESPONSE, r);
					app.alert(R.string.response_sent_message);
				}
				break;
				
			case ENDPOINTS_SEND_FEEDBACK:
				Response res = EndpointsUtils.fromBundle(extras, Response.class);
				if (res!=null) {
					app.setData(DataKey.FILE_RESPONSE, res);
					app.alert(R.string.file_feedback_sent_message);
				}
				break;
				
			case GCM_SEND_REQUEST_CHAT:
				if (activity instanceof RequestFileActivity) {
					IntentAction.GCM_REQUEST_CHAT_SENT.start(
							activity, intent.getExtras(), 
							Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
				} else {
					//TODO: notify?
				}
				break;
				
			case GCM_SEND_RESPONSE_CHAT:
				if (activity instanceof ResponseFileActivity) {
					IntentAction.GCM_RESPONSE_CHAT_SENT.start(
							activity, intent.getExtras(), 
							Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
				} else {
					//TODO: notify?
				}
				break;
				
			default:
			}
		
	}
}
